package com.vogue.supplymall.shop.domain.mapper;

import com.vogue.ssm.database.util.SqlLangDriver;
import com.vogue.supplymall.common.domain.dto.SearchInfo;
import com.vogue.supplymall.shop.domain.model.Shop;
import com.vogue.supplymall.shop.domain.provider.ShopSqlProvider;

import org.apache.ibatis.annotations.Lang;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;

import java.util.List;
import java.util.Map;

/**
 * The type Shop mapper.
 */
@Mapper
public interface ShopMapper extends BaseMapper<Shop, Long> {

    /**
     * The constant sql.
     */
    String sql = null;

    /**

     * Find by id shop.
     *
     * @param id the id
     * @return the shop
     */
    @Select("SELECT s.id,s.`name`,s.organid,s.syscodeid,s.sign,s.sendamount,s.address,s.tel,s.linkman,s.area,s.province,s.city,s.entname " +
            "FROM shop s WHERE s.disabledflag = 0 AND s.hiddenflag = 0 AND id = #{id}")
    Shop findById(Long id);

    /**
     * 通过唯一注册号id和对应机构id，得到店铺信息
     *
     * @param sysCodeId the sys code id
     * @param organId   the organ id
     * @return the shop
     */
    @Select("select * from shop s where organid = #{arg1} and syscodeid=#{arg0}")
    @Lang(SqlLangDriver.class)
    List<Shop> findShop(Long sysCodeId, Long organId);
    
    /**
     * 通过唯一注册号id和对应机构dmatchid，得到店铺信息
     *
     * @param sysCodeId the sys code id
     * @param organId   the organ id
     * @return the shop
     */
    @Select("select * from shop s where matchId = #{arg1} and syscodeid=#{arg0} and tendid=#{arg2} and opendate is not null")
    @Lang(SqlLangDriver.class)
    List<Shop> findShopByMS(Long sysCodeId, String matchId,int tendId);

    /**
     * Find shop by sql list.
     * 根据条件检索店铺
     *
     * @param searchInfo the search info
     * @return the list
     * @throws RuntimeException the runtime exception
     */
    @SelectProvider(type = ShopSqlProvider.class, method = "find")
    List<Map<String, Object>> findShopBySql(SearchInfo searchInfo) throws RuntimeException;

    /**
     * 查找满足条件的总店铺的数量
     * 
     * @param searchInfo
     * @return
     * @throws RuntimeException
     */
    @SelectProvider(type = ShopSqlProvider.class, method = "findCount")
    Long findShopCountBySql(SearchInfo searchInfo) throws RuntimeException;
    
    /**
     * Count valid shop long.
     * dmin-business使用，查询所有开通的店铺
     *
     * @return the long
     */
    @Select("select COUNT(DISTINCT syscodeid) from shop where disabledflag = 0;")
    Long countValidShop();
}